package com.imooc.service.impl;

import com.imooc.dataobject.ProductInfo;
import com.imooc.dto.CartDTO;
import com.imooc.enums.ProductStatusEnum;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.SellException;
import com.imooc.repository.ProductInfoRepository;
import com.imooc.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoRepository productInfoRepository;

    @Override
    public ProductInfo findOne(String productId) {
        return productInfoRepository.findById(productId).get();
    }

    @Override
    public Page<ProductInfo> findUpAll(Pageable pageable) {
        List<ProductInfo> productInfoList = new ArrayList<>();
        List<ProductInfo> content = productInfoRepository.findAll(pageable).getContent();
        for (ProductInfo productInfo : content) {
            if (productInfo.getProductStatus() == ProductStatusEnum.UP.getCode()) {
                productInfoList.add(productInfo);
            }
        }
        return new PageImpl<>(productInfoList, pageable, productInfoList.size());
    }

    @Override
    public List<ProductInfo> findUpAll() {
        List<ProductInfo> productInfoList = new ArrayList<>();
        List<ProductInfo> productInfoList1 = productInfoRepository.findAll();
        for (ProductInfo productInfo : productInfoList1) {
            if (productInfo.getProductStatusEnum().equals(ProductStatusEnum.UP)) {
                productInfoList.add(productInfo);
            }
        }
        return productInfoList;
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        return productInfoRepository.findAll(pageable);
    }

    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return productInfoRepository.save(productInfo);
    }

    @Override
    public void delete(String productId) {
        productInfoRepository.deleteById(productId);
    }

    @Override
    public void onSale(String productId) {
        ProductInfo productInfo = productInfoRepository.findById(productId).get();
        if (productInfo == null) {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if (productInfo.getProductStatusEnum() == ProductStatusEnum.UP) {
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
        productInfoRepository.save(productInfo);
    }

    @Override
    public void offSale(String productId) {
        ProductInfo productInfo = productInfoRepository.findById(productId).get();
        if (productInfo == null) {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if (productInfo.getProductStatusEnum() == ProductStatusEnum.DOWN) {
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
        productInfoRepository.save(productInfo);
    }

    @Override
    public void decreaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO : cartDTOList) {
            ProductInfo productInfo = productInfoRepository.findById(cartDTO.getProductId()).get();
            if (productInfo == null) {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer currentQuantity = productInfo.getProductStock() - cartDTO.getProductQuantity();
            if (currentQuantity < 0) {
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            productInfo.setProductStock(currentQuantity);
            productInfoRepository.save(productInfo);
        }
    }

    @Override
    public void increaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO : cartDTOList) {
            ProductInfo productInfo = productInfoRepository.findById(cartDTO.getProductId()).get();
            if (productInfo == null) {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer currentQuantity = productInfo.getProductStock() + cartDTO.getProductQuantity();
            productInfo.setProductStock(currentQuantity);
            productInfoRepository.save(productInfo);
        }
    }
}
